<!-- 字典编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="460"
    v-model="visible"
    :title="actionConfigs[action].title"
    @open="handleOpen"
    @close="handleCancel"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.prevent=""
    >
      <el-form-item
        v-if="actionConfigs[action].showFields.includes('dict_name')"
        label="字典名称"
        prop="dict_name"
      >
        <el-input
          clearable
          :maxlength="20"
          v-model="form.dict_name"
          placeholder="请输入字典名称"
          :disabled="actionConfigs[action].disabledFields.includes('dict_name')"
        />
      </el-form-item>
      <el-form-item
        v-if="actionConfigs[action].showFields.includes('dict_code')"
        label="字典值"
        prop="dict_code"
      >
        <el-input
          clearable
          :maxlength="20"
          v-model="form.dict_code"
          placeholder="请输入字典值"
          :disabled="actionConfigs[action].disabledFields.includes('dict_code')"
        />
      </el-form-item>
      <el-form-item
        v-if="actionConfigs[action].showFields.includes('sort_number')"
        label="排序号"
        prop="sort_number"
      >
        <el-input-number
          :min="0"
          :max="9999"
          v-model="form.sort_number"
          placeholder="请输入排序号"
          controls-position="right"
          class="ele-fluid"
          :disabled="
            actionConfigs[action].disabledFields.includes('sort_number')
          "
        />
      </el-form-item>
      <el-form-item
        v-if="actionConfigs[action].showFields.includes('comments')"
        label="备注"
      >
        <el-input
          :rows="4"
          type="textarea"
          :maxlength="200"
          v-model="form.comments"
          placeholder="请输入备注"
          :disabled="actionConfigs[action].disabledFields.includes('comments')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit">
        {{ actionConfigs[action].button }}
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { useFormEdit } from "@/hooks/useFormEdit";
import {
  addDictionary,
  updateDictionary,
  showDictionary,
} from "@/api/system/dictionary";

const props = defineProps({
  action: {
    type: String,
    default: "edit",
    validator: (value) => ["add", "edit", "view"].includes(value),
  },
  data: Object,
  modelValue: Boolean,
});

const emit = defineEmits(["update:modelValue", "done"]);
const actionConfigs = {
  add: {
    title: "新增字典数据",
    button: "新增",
    api: addDictionary, // 补充add的API,
    showFields: ["dict_name", "dict_code", "sort_number", "comments"],
    disabledFields: [],
    rules: {
      dict_name: [
        {
          required: true,
          message: "请输入字典名称",
          type: "string",
          trigger: "blur",
        },
      ],
      dict_code: [
        {
          required: true,
          message: "请输入字典值",
          type: "string",
          trigger: "blur",
        },
      ],
      sort_number: [
        {
          required: true,
          message: "请输入排序号",
          type: "number",
          trigger: "blur",
        },
      ],
    },
  },
  edit: {
    title: "修改字典数据",
    button: "保存",
    api: updateDictionary, // 补充edit的API
    showFields: ["dict_name", "dict_code", "sort_number", "comments"],
    disabledFields: [],
    rules: {
      dict_name: [
        {
          required: true,
          message: "请输入字典名称",
          type: "string",
          trigger: "blur",
        },
      ],
      dict_code: [
        {
          required: true,
          message: "请输入字典值",
          type: "string",
          trigger: "blur",
        },
      ],
      sort_number: [
        {
          required: true,
          message: "请输入排序号",
          type: "number",
          trigger: "blur",
        },
      ],
    },
  },
  view: {
    title: "查看字典数据",
    button: "确定",
    // api: showDictionaryData, // 补充view的API
    showFields: ["dict_name", "dict_code", "sort_number", "comments"],
    disabledFields: ["dict_name", "dict_code", "sort_number", "comments"],
    rules: {},
  },
};
const {
  form,
  formRef,
  rules,
  visible,
  loading,
  handleOpen,
  handleSubmit,
  handleCancel,
} = useFormEdit(props, emit, {
  actionConfigs,
  formFields: {
    dict_id: void 0,
    dict_name: "",
    dict_code: "",
    sort_number: void 0,
    comments: "",
  },

  detailApi: showDictionary,
  idField: "dict_id",
});
</script>
